<template>
  <div id="payAddDeduction">
    <div class="title"></div>
    <div class="content">
      <div class="pay_add_deduction" v-if="oInitAddDeduction">
        <div class="pay_add_deduction_content">
          <div class="title">What deductions or contributions does {{oInitAddDeduction?oInitAddDeduction.personName:''}} have?</div>
          <div class="content" v-if="oInitAddDeduction">
            <div class="select_item">
              <div class="title">Deduction/contribution or garnishment</div>
              <div>
                <el-select style="width: 180px" v-model="deductionBigType" @change="deductionBigTypeChange">
                  <template v-if="oInitAddDeduction.deductionBigType">
                    <el-option v-for="(item,key) in oInitAddDeduction.deductionBigType" :key="key"  :label="item.showName" :value="item.value"></el-option>
                  </template>
                </el-select>
              </div>
            </div>
            <div class="select_item" v-if="deductionSmallTypeName">
              <div class="title">{{deductionSmallTypeName}}</div>
              <el-select v-if="deductionBigType == 2" style="width: 180px" v-model="deductionId">
                <template v-if="oInitAddDeduction.garnishmentSmallTypes">
                  <el-option v-for="(item,key) in oInitAddDeduction.garnishmentSmallTypes" :key="key"  :label="item.showName" :value="item.deductionId"></el-option>
                </template>
              </el-select>
              <el-select v-if="deductionBigType == 1" style="width: 180px" v-model="deductionId">
                <template v-if="oInitAddDeduction.deductionSmallTypes">
                  <el-option v-for="(item,key) in oInitAddDeduction.deductionSmallTypes" :key="key"  :label="item.showName" :value="item.deductionId"></el-option>
                </template>
              </el-select>
            </div>
          </div>
          <div class="small_type_info" v-if="deductionSmallTypeInfo">
            <div class="description">
              <label class="info_label">Description (appears on paycheck)</label>
              <el-input v-model="deductionModel.description" style="width: 180px"></el-input>
            </div>
            <el-row class="info">

              <el-col :span="12" >
                <p class="title">Employee deduction</p>
                <el-row class="info_row">
                  <p class="info_label">Amount per pay period</p>
                  <el-col :span="10">
                    <el-select v-model="deductionModel.employeeAmountType" style="width: 180px">
                      <template v-if="oInitAddDeduction.deductionAmountType">
                        <el-option v-for="(item,key) in oInitAddDeduction.deductionAmountType" :key="key"  :label="item.showName" :value="item.value"></el-option>
                      </template>
                    </el-select>
                  </el-col>
                  <el-col :span="14" v-if="deductionModel.employeeAmountType">
                    <el-row>
                      <el-col :span="2" style="text-align: right" v-if="deductionModel.employeeAmountType == 1" >$</el-col>
                      <el-col :span="10" v-if="deductionModel.employeeAmountType == 1">
                        <el-input-number   v-model="deductionModel.employeeAmount" size="small" :min="0" :controls="false"  style="width: 100px;margin-left: 10px"></el-input-number>
                      </el-col>
                      <el-col :span="10" v-if="deductionModel.employeeAmountType == 2">
                        <el-input-number   v-model="deductionModel.employeeAmount" size="small" :min="0" :max="100" :controls="false"  style="width: 100px;margin-left: 10px"></el-input-number>
                      </el-col>
                      <el-col :span="2" style="text-align: right"  v-if="deductionModel.employeeAmountType == 2">%</el-col>
                    </el-row>
                  </el-col>
                </el-row>
                <el-row class="info_row">
                  <label class="info_label">Annual maximum (Optional)</label>
                  <el-input disabled v-model="deductionModel.employeeAnnualMax" style="width: 180px"></el-input>
                </el-row>
              </el-col>
              <el-col :span="12" v-if="deductionSmallTypeInfo.isEmployerEnabled == 1">
                <p class="title">Company-paid contribution</p>
                <el-row class="info_row">
                  <p class="info_label">Amount per pay period</p>
                  <el-col :span="10">
                    <el-select v-model="deductionModel.companyAmountType" style="width: 180px">
                      <template v-if="oInitAddDeduction.deductionAmountType">
                        <el-option v-for="(item,key) in oInitAddDeduction.deductionAmountType" :key="key"  :label="item.showName" :value="item.value"></el-option>
                      </template>
                    </el-select>
                  </el-col>
                  <el-col :span="14" v-if="deductionModel.companyAmountType">
                    <el-row>
                      <el-col :span="2" style="text-align: right" v-if="deductionModel.companyAmountType == 1" >$</el-col>
                      <el-col :span="10" v-if="deductionModel.companyAmountType == 1">
                        <el-input-number   v-model="deductionModel.companyAmount" size="small" :min="0" :controls="false"  style="width: 100px;margin-left: 10px"></el-input-number>
                      </el-col>
                      <el-col :span="10" v-if="deductionModel.companyAmountType == 2">
                        <el-input-number   v-model="deductionModel.companyAmount" size="small" :min="0" :max="100" :controls="false"  style="width: 100px;margin-left: 10px"></el-input-number>
                      </el-col>
                      <el-col :span="2" style="text-align: right"  v-if="deductionModel.companyAmountType == 2">%</el-col>
                    </el-row>
                  </el-col>
                </el-row>

                <el-row class="info_row" v-if="deductionModel.companyAnnualMax!=0">
                  <label class="info_label">Annual maximum (Optional)</label>
                  <el-input v-model="deductionModel.companyAnnualMax" style="width: 180px"></el-input>
                </el-row>
              </el-col>
            </el-row>
          </div>
        </div>
        <div class="pay_add_deduction_btn_group">
          <el-button type="primary" :disabled="!isCanSubmit" @click="submit">Done</el-button>
          <el-button @click="$router.back()">Cancel</el-button>
        </div>
      </div>
    </div>
  </div>
 <!-- <div class="loading_content" v-else v-loading="true"  element-loading-text="loading"></div>-->
</template>

<script>
  export default {
    components:{
    },
    data:function(){
      return{
        //数据Model
        deductionId:null,
        deductionBigType:null,
        deductionModel:{
          deductionBigType:null,
          deductionCode:null,
          deductionId:null,
          description:null,
          employeeAmountType:null,
          employeeAmount:null,
          employeeAnnualMax:null,
          companyAmountType:null,
          companyAmount:null,
          companyAnnualMax:null
        },
        deductionSmallTypeName:"",
        deductionSmallTypeInfo:null
      }
    },
    created:function(){
     this.$store.dispatch('initAddDeduction',{personId:this.getPersonId()});
    },
    activated:function(){
      //this.$store.dispatch('initAddDeduction',{personId:this.getPersonId()});
    },
    deactivated:function () {
      this.clearData();
    },
    computed:{
      oInitAddDeduction(){
        return this.$store.getters.oInitAddDeduction;
      },
      isCanSubmit(){
        return this.deductionModel.deductionBigType&&this.deductionModel.deductionId&&this.deductionModel.description
      }
    },
    watch:{
      'deductionId':function (value) {
        if(!value) return;
        this.deductionSmallTypeInfo = this.oInitAddDeduction.deductionSmallTypes.filter(function(item){
            return item.deductionId == value;
          })[0] || null;
        if(!this.deductionSmallTypeInfo) return;
        this.deductionModel = {
          deductionBigType:this.deductionBigType || this.deductionSmallTypeInfo.deductionBigType,
          deductionCode:this.deductionSmallTypeInfo.deductionCode,
          deductionId:this.deductionSmallTypeInfo.deductionId,
          description:this.deductionSmallTypeInfo.description,
          employeeAmountType:this.deductionSmallTypeInfo.employeeAmountType,
          employeeAmount:this.deductionSmallTypeInfo.employeeAmount,
          employeeAnnualMax:this.deductionSmallTypeInfo.employeeAnnualMax,
          companyAmountType:this.deductionSmallTypeInfo.employerAmountType,
          companyAmount:this.deductionSmallTypeInfo.employerAmount,
          companyAnnualMax:this.deductionSmallTypeInfo.employerAnnualMax,
        }
      },
      'deductionBigType':function (value) {
        if(!value) return;
        this.deductionId = this.deductionModel.deductionId = null;
        this.deductionSmallTypeInfo = null;
        this.deductionSmallTypeName = this.oInitAddDeduction.deductionBigType.filter(function(item){
            return item.value == value;
          })[0].showName || "";
      }
    },
    mounted:function(){
    },
    methods:{
      getPersonId(){
        return this.$route.params.id;
      },
      clearData:function () {
        this.deductionId = null;
        this.deductionBigType = null;
        this.deductionModel ={
          deductionBigType:null,
            deductionCode:null,
            deductionId:null,
            description:null,
            employeeAmountType:null,
            employeeAmount:null,
            employeeAnnualMax:null,
            companyAmountType:null,
            companyAmount:null,
            companyAnnualMax:null
        },
        this.deductionSmallTypeName ="";
        this.deductionSmallTypeInfo = null;
      },
      /**
       * bigType下拉框改变
       */
      deductionBigTypeChange(value){

      },
      /**
       * smallType下拉框改变
       */
      deductionSmallTypeChange(value){

      },
      /**
       * 提交
       */
      submit(){
        var t = this;
        t.deductionModel.personId = this.getPersonId();
        t.$store.dispatch('saveDeduction',t.deductionModel).then(function () {
          t.$message.success('save success');
          t.$router.back();
        });
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style  rel="stylesheet/scss"  type="text/css" lang="scss" scoped>

  @import "../../assets/css/variable";
  #payAddDeduction{
    >.content{
      padding: 0;
    }
  }
  .pay_add_deduction {
    min-height: 100%;
    background-color: #fff;
    padding: 50px;
    box-sizing: border-box;
    .pay_add_deduction_content {
      >.title{
        font-size: 20px;
        line-height: 50px;
        color: #333
      }
      .select_item{
         .title{
           line-height: 50px;
           font-weight: bold;
         }
      }
      .small_type_info{
        .description{
          border-bottom: 1px #ddd solid;
          padding: 10px 0;
        }
        .info_label{
          display: block;
          height: 40px;
          line-height:40px;
        }
        .info{
          .title{
            line-height: 40px;
            font-weight: bold;
          }
          .info_row{
            margin-bottom: 10px;
            line-height: 30px;
          }
        }
      }
    }
    .pay_add_deduction_btn_group{
      margin: 20px 0;
    }
  }
</style>
